package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClientCase;
import cn.wolfcode.crm.mapper.ClientCaseMapper;
import cn.wolfcode.crm.query.impl.ClientCaseQuery;
import cn.wolfcode.crm.service.IClientCaseService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("clientCase")
public class ClientCaseController {
    @Autowired
    IClientCaseService clientCaseService;
    @Autowired
    ClientCaseMapper clientCaseMapper;

    @RequestMapping("view")
    @RequiresPermissions(value = {"clientCase:view","客户明细页面"},logical= Logical.OR)
    public String view() {
        return "clientCase";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<ClientCase> getAll() {
        return clientCaseService.getAll();
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"clientCase:save","保存客户明细信息"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(ClientCase clientCase){
        try {
            clientCaseService.saveOrUpdate(clientCase);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ClientCaseQuery qo) {
        return clientCaseService.query(qo);
    }
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载响应头
        response.setHeader("Content-disposition", "attachment;filename=clientCase.xls");
        //创建excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("day01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("背锅侠");
        row.createCell(1).setCellValue("保险机构");
        row.createCell(2).setCellValue("疑问");
        row.createCell(3).setCellValue("意见");
        row.createCell(4).setCellValue("联系电话");
        row.createCell(5).setCellValue("理赔金额");
        row.createCell(6).setCellValue("事发地址");
        row.createCell(7).setCellValue("客户名");
        row.createCell(8).setCellValue("损失金额");
        row.createCell(9).setCellValue("保单号");
        row.createCell(10).setCellValue("车牌号");
        row.createCell(11).setCellValue("事故时间");
        //查询员工数据
        List<ClientCase> list = clientCaseMapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            ClientCase employee = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getWok());
            row.createCell(1).setCellValue(employee.getOrganization());
            row.createCell(2).setCellValue(employee.getProblem());
            row.createCell(3).setCellValue(employee.getOpinion());
            row.createCell(4).setCellValue(employee.getTel());
            row.createCell(5).setCellValue(employee.getMoney());
            row.createCell(6).setCellValue(employee.getPlace());
            row.createCell(7).setCellValue(employee.getClientname());
            row.createCell(8).setCellValue(employee.getLossmoney());
            row.createCell(9).setCellValue(employee.getNumbering());
            row.createCell(10).setCellValue(employee.getCarnumber());
//            row.createCell(11).setCellValue(employee.getTime());
            CreationHelper createHelper = workbook.getCreationHelper();
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(
                    createHelper.createDataFormat().getFormat("m/d/yy"));
            HSSFCell cellDate = row.createCell(11);
            cellDate.setCellStyle(cellStyle);
            cellDate.setCellValue(employee.getTime());

        }
        //写入数据
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
